#ifndef PARAMETERSQLDB_H
#define PARAMETERSQLDB_H

#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMutex>
#include <QSqlError>
class SQLitedPool {
public:
    static SQLitedPool *getInstance() {
        static SQLitedPool instance;
        return &instance;
    }

    QSqlDatabase *getDataBase() { return m_db; }

private:
    SQLitedPool() {
        m_db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", "ProcessDB"));
        m_db->setDatabaseName("./process_data.db");
        if (!m_db->open()) {
            qDebug() << "数据库连接失败：" << m_db->lastError().text();
        }
    }

    ~SQLitedPool() {
        if (m_db->isOpen()) m_db->close();
        delete m_db;
    }

    QSqlDatabase *m_db = nullptr;
};

class ParameterSQLDB : public QObject
{
    Q_OBJECT
public:
    explicit ParameterSQLDB(QObject *parent = nullptr);
    bool execSql(const QString &sql);
    QSqlQuery *execSqlResult(const QString &sql);

private:
    QSqlDatabase *m_db = SQLitedPool::getInstance()->getDataBase();
    QMutex m_mutex;
};

#endif // PARAMETERSQLDB_H
